﻿Imports System.Data.OleDb
Imports System.IO
Imports System.IO.File
Imports System.Data
Imports System.Data.SqlClient
Public Class 螺丝扫码管理
    Private Sub 螺丝扫码管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Custom_alarm_editing()
    End Sub

    ''' 利用界面上的查询按钮
    ''' 利用SQL语句查询数据库内容，查询数据库中的所有数据，并将数据库的内容写入到datagridview控件
    ''' 该函数是操作报警记录报表
    ''' <remarks></remarks>
    Private Sub Custom_alarm_editing()
        Try
            Dim sql As String = "select * from Scan_screw"
            Dim ds As DataSet = SearchMysql(sql)
            DataGridView4.DataSource = ds.Tables(0)
            Me.DataGridView4.Columns(0).Width = 80
            Me.DataGridView4.Columns(1).Width = 180
            Me.DataGridView4.Columns(2).Width = 180
            Me.DataGridView4.Columns(3).Width = 180
            Me.DataGridView4.Columns(4).Width = 80
            Me.DataGridView4.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable                       '禁止列排序
            Me.DataGridView4.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable                       '禁止列排序
            Me.DataGridView4.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable                       '禁止列排序
            Me.DataGridView4.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable                       '禁止列排序
            Me.DataGridView4.Columns(4).SortMode = DataGridViewColumnSortMode.NotSortable                       '禁止列排序
            DataGridView4.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter                '字体居中
            DataGridView4.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter   '标题居中
            DataGridView4.AllowUserToAddRows = False                                                            '清空最后空白行
        Catch ex As Exception
            MsgBox(ex.Message.ToString & vbCrLf & ex.StackTrace.ToString)
        End Try
    End Sub


    ''' 自定义报警新建添加
    ''' 
    ''' 
    ''' <remarks></remarks>
    Private Sub Custom_ADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Custom_ADD.Click
        Try
            If Custom_English.Text <> "" And Custom_Cause.Text <> "" And Screw_Mode.Text <> "" And Feeder.Text <> "" Then
                Dim sql As String = "insert into Scan_screw(模板名称,SN,螺丝名称,供料器号) values(" & "'" & Custom_Cause.Text & "'" & "," & "'" & Custom_English.Text & "'" & "," & "'" & Screw_Mode.Text & "'" & "," & "'" & Feeder.Text & "'" & ")"
                HandleMysql(sql)
                Custom_alarm_editing()
                Custom_English.Text = ""
                Custom_Cause.Text = ""
                Screw_Mode.Text = ""
                Feeder.Text = ""
                Custom_ID.Text = ""
            Else
                MsgBox("必要信息为空，添加失败。")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString & vbCrLf & ex.StackTrace.ToString)
        End Try
    End Sub

    ''' 设置ID号输入框只能输入数字
    ''' 
    ''' 
    ''' <remarks></remarks>
    Private Sub Custom_ID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Custom_ID.KeyPress
        If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    ''' 设置ID号输入框只能输入数字
    ''' 
    ''' 
    ''' <remarks></remarks>
    Private Sub Feeder_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Feeder.KeyPress
        If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    ''' 自定义报警根据ID号删除
    ''' 
    ''' 
    ''' 
    ''' <remarks></remarks>
    Private Sub Custom_deleting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Custom_deleting.Click
        Try
            If Custom_ID.Text <> "" Then
                Dim sql As String = "delete from Scan_screw where ID =" & CInt(Custom_ID.Text)
                HandleMysql(sql)
                Custom_alarm_editing()
            Else
                MsgBox("ID号不能为空。")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString & vbCrLf & ex.StackTrace.ToString)
        End Try
    End Sub
    ''' 自定义报警根据ID号修改
    ''' 
    ''' 
    ''' 
    ''' <remarks></remarks>
    Private Sub Custom_modification_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Custom_modification.Click
        Try
            If Custom_English.Text <> "" And Custom_Cause.Text <> "" And Screw_Mode.Text <> "" And Feeder.Text <> "" Then
                Dim sql As String = "update Scan_screw set SN ='" & Custom_Cause.Text & "'" & "," & "模板名称 ='" & Custom_Cause.Text & "'" & "," & "螺丝名称 ='" & Screw_Mode.Text & "'" & "," & "供料器号 ='" & Feeder.Text & "'" & "where ID =" & CInt(Custom_ID.Text)
                HandleMysql(sql)
                Custom_alarm_editing()
                Custom_English.Text = ""
                Custom_Cause.Text = ""
                Screw_Mode.Text = ""
                Feeder.Text = ""
                Custom_ID.Text = ""
            Else
                MsgBox("必要信息为空，修改失败。")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString & vbCrLf & ex.StackTrace.ToString)
        End Try
    End Sub

    Private Sub DataGridView4_Click(sender As System.Object, e As System.EventArgs) Handles DataGridView4.Click
        Try
            Dim q As Integer = DataGridView4.CurrentRow.Index
            Dim l As String = DataGridView4(0, q).Value.ToString
            Dim sql As String = "Select * from Scan_screw where ID = " & l
            Dim ds As DataSet = SearchMysql(sql)
            If ds.Tables(0).Rows.Count > 0 Then
                Custom_ID.Text = ds.Tables(0).Rows(0)(0).ToString
                Custom_Cause.Text = ds.Tables(0).Rows(0)(1).ToString
                Custom_English.Text = ds.Tables(0).Rows(0)(2).ToString
                Screw_Mode.Text = ds.Tables(0).Rows(0)(3).ToString
                Feeder.Text = ds.Tables(0).Rows(0)(4).ToString
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)

    End Sub
End Class